﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;

namespace CarLoanSystem
{
    public partial class panelMember : UserControl
    {
        private static EmployeeItem empItem;
        private static string mode;
        public panelMember()
        {
            InitializeComponent();
            this.gvMember.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentClick);
            genGridview();
        }

        public void setEmployee(EmployeeItem item) 
        {
            empItem = item;
        }

        public void setMode(string item)
        {
            mode = item;
        }

        private void genGridview()
        {
            // gen Grideview
            gvMember.AutoGenerateColumns = false;

            //Set Columns Count
            gvMember.ColumnCount = 6;

            //Add Columns
            gvMember.Columns[1].Name = "CUSTOMER_ID";
            gvMember.Columns[1].HeaderText = "รหัสสมาชิก";
            gvMember.Columns[1].DataPropertyName = "CUSTOMER_ID";

            gvMember.Columns[2].Name = "CUSTOMER_NAME";
            gvMember.Columns[2].HeaderText = "ชื่อ";
            gvMember.Columns[2].DataPropertyName = "CUSTOMER_NAME";

            gvMember.Columns[3].Name = "CUSTOMER_LASTNAME";
            gvMember.Columns[3].HeaderText = "นามสกุล";
            gvMember.Columns[3].DataPropertyName = "CUSTOMER_LASTNAME";

            gvMember.Columns[4].Name = "CUSTOMER_TEL";
            gvMember.Columns[4].HeaderText = "โทรศัพท์";
            gvMember.Columns[4].DataPropertyName = "CUSTOMER_TEL";


            gvMember.Columns[5].Name = "CUSTOMER_BLACK_LIST";
            gvMember.Columns[5].HeaderText = "สถานะแบล็คลิส";
            gvMember.Columns[5].DataPropertyName = "CUSTOMER_BLACK_LIST";
            // end gen gridview
            gvMember.DataSource = LoadCustomer();
        }

        private DataTable LoadCustomer()
        {
            DataTable dt = DAL.CustomerInfoDAL.LoadAllCustomer(this.txtId.Text, this.txtName.Text, this.txtLastName.Text);
            return dt;
        }

        private void CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
            {
                frmMemberDetail frmMemberDetail = new frmMemberDetail(Convert.ToInt32(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString()), mode, empItem);
                frmMemberDetail.ShowDialog();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            gvMember.DataSource = LoadCustomer();
        }
    }
}
